<?php
class Tour
{
	private $_table_tour = 'tours';
	private $_table_categories_tours = 'categories_tours';
	private $_table_tour_fields = array('tour_id', 'tour_name', 'tour_title', 'tour_alias', 'tour_intro', 'tour_details', 'tour_image', 'tour_no', 'tour_price', 'tour_price_details', 'tour_duration', 'tour_departure', 'tour_contact', 'meta_keywords', 'meta_description', 'user_id', 'hits', 'created', 'updated', 'ordering', 'active');
	static protected $_instance = null;
	
	/**
	 * Get object instance
	 */
	
	public static function getInstance()
	{
		if( null === self::$_instance ) {
			self::$_instance = new self();	
		}
		return self::$_instance;
	}
	
	/**
	 * Get tour
	 * @param int $id
	 */
	
	public function getTour($id, $active = 1)
	{
		$storage = My_Globals::getStorage();
		$select = $storage->select()
						->from($this->_table_tour, '*')
						->where('tour_id = ?', $id);						
		if($active > 0)
		{
			$select->where('active = ?', $active);
		} 
		return $storage->fetchRow($select);
	}
	
	/**
	 * Get tour by alias
	 * @param string $alias
	 */
	
	public function getTourByAlias($alias, $active = 1)
	{
		$storage = My_Globals::getStorage();
		$select = $storage->select()
						->from($this->_table_tour, '*')
						->where($storage->quoteInto('tour_alias = ?', $alias));
		if($active > 0) {
			$select = $storage->where('active = ?', $active);
		}
		return $storage->fetchRow($select);
	}
	
	/**
	 * Delete tour
	 * @param int | array $id
	 */
	
	public function deleteTour($id)
	{
		$storage = My_Globals::getStorage();
		return $storage->delete($this->_table_tour, $storage->quoteInto('tour_id IN (?)', $id));
	}
	
	/**
	 * Save tour
	 * @param array $data
	 */
	
	public function saveTour($data)
	{
		$storage = My_Globals::getStorage();
		$data = My_Globals::initData($this->_table_tour_fields, $data);
		$rs = false;
		if(isset($data['tour_id']) && $data['tour_id'] > 0) {
			// Update
			$storage->update($this->_table_tour, $data, 'tour_id='.$data['tour_id']);
			$rs = $data['tour_id'];
		} else {
			// Insert
			$rs = $storage->insert($this->_table_tour, $data);
		}
		// Clean
		unset($data);
		// Return
		return $rs;
	}
	
	/**
	 * Get tour list
	 * @param int $page
	 * @param int $limit
	 * @param string $where
	 * @param string $order
	 */
	
	public function getTourList($page, $limit, $where = null, $order = null)
	{
		$storage = My_Globals::getStorage();
		$select = $storage->select();
		$select->from($this->_table_tour, '*');
		//$select->joinLeft('categories_tours', $this->_table_tour.'.tour_id = categories_tours.tour_id');
		if($page == 0) $page = 1;
		$select->limitPage($page, $limit);
		if($where != null) {
			$select->where($where);
		}
		if($order != null) {
			$select->order($order);
		} else {
			$select->order($this->_table_tour.'.tour_id ASC');
		}
		return $storage->fetchAll($select);
	}
	
	/**
	 * Get tour amount
	 */
	
	public function getAmount($where = null)
	{
		$storage = My_Globals::getStorage();
		$select = $storage->select();
		$select->from($this->_table_tour, 'COUNT(*) AS total');
		//$select->joinLeft('categories_tours', $this->_table_tour.'.tour_id = categories_tours.tour_id');
		if($where) $select->where($where);
		$row = $storage->fetchRow($select);
		return $row['total'];
	}
	
	/**
	 * Check tour alias exist
	 * @param string $alias
	 */
	
	public function checkTourAliasExist($alias)
	{
		$storage = My_Globals::getStorage();
		$select = $storage->select()
						->from($this->_table_tour, 'COUNT(*) AS total')
						->where($storage->quoteInto('tour_alias = ?', $alias));
		$row = $storage->fetchRow($select);
		if($row['total'] > 0) {
			return true;
		}
		return false;
	}
	
	/**
	 * 
	 * Save categories tours
	 * @param int $tourId
	 * @param array $categories
	 */
	public function saveCategoriesTours($tourId, $categories)
	{
		if($tourId == 0 || !is_array($categories)) {
			return false;
		}
		$storage = My_Globals::getStorage();
		$storage->delete($this->_table_categories_tours, 'tour_id='. $tourId);
		// Remove duplicate values
		$categories = array_unique($categories);
		foreach($categories as $cId) {
			if($cId > 0) {
				$data = array('category_id' => $cId, 'tour_id' => $tourId);
				$storage->insert($this->_table_categories_tours, $data);
			}
		}
		return true;
	}
	
	/**
	 * 
	 * Get categories by tour id
	 * @param int $tourId
	 */
	public function getCategoriesByTourId($tourId)
	{
		if($tourId == 0) return false;
		$storage = My_Globals::getStorage();
		$select = $storage->select()
						->from($this->_table_categories_tours, 'category_id')
						->where('tour_id = ?', $tourId);
		$list = $storage->fetchAll($select);
		$data = array();
		if(count($list)) {
			foreach($list as $r) {
				$data[] = $r['category_id'];
			}
		}
		unset($list);
		return $data;
	}
}